package cn.wolfcode.web.controller;


import cn.hutool.core.date.DateUnit;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.RandomUtil;
import cn.wolfcode.domain.Appointment;
import cn.wolfcode.domain.Business;
import cn.wolfcode.domain.SystemDictionary;
import cn.wolfcode.domain.SystemDictionaryItem;
import cn.wolfcode.qo.AppointmentQueryObject;
import cn.wolfcode.service.IAppointmentService;
import cn.wolfcode.qo.QueryObject;
import cn.wolfcode.service.IBusinessService;
import cn.wolfcode.service.ISystemDictionaryItemService;
import cn.wolfcode.service.ISystemDictionaryService;
import cn.wolfcode.util.ObjectEnum;
import cn.wolfcode.util.Request;

import com.github.pagehelper.PageInfo;
import lombok.Data;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Date;
import java.util.List;

@Controller
@RequestMapping("/appointment")
public class AppointmentController {

    @Autowired
    private IAppointmentService appointmentService;

    @Autowired
    private ISystemDictionaryItemService systemDictionaryItemService;

    @Autowired
    ISystemDictionaryService systemDictionaryService;

    @Autowired
    private IBusinessService businessService;

    @Request(name="业务预约大类页面",expression="appointment:list")
    @RequestMapping("/list")
    public String list(Model model, @ModelAttribute("qo") AppointmentQueryObject qo,Long id){
        PageInfo<Appointment> pageInfo = appointmentService.query(qo);
        model.addAttribute("pageResult", pageInfo);
        Appointment appointment = appointmentService.get(id);
        model.addAttribute("appointment",appointment);
        List<SystemDictionaryItem> systemDictionaryItems = systemDictionaryItemService.listAll();
        model.addAttribute("systemDictionaryItems",systemDictionaryItems);
        List<Business> businesses = businessService.listAll();
        model.addAttribute("businesses",businesses);
        model.addAttribute("ObjectEnum",ObjectEnum.values());
        List<SystemDictionary> systemDictionaries = systemDictionaryService.selectBusiness(qo);
        model.addAttribute("systemDictionaries",systemDictionaries);
        return "appointment/list";
    }


    @Request(name="业务预约大类删除",expression="appointment:delete")
    @RequestMapping("/delete")
    public String delete(Long id) {
        if (id != null) {
           appointmentService.delete(id);
        }
        return "redirect:/appointment/list";
    }


    @Request(name="业务预约大类新增/编辑",expression="appointment:saveOrUpdate")
    @RequestMapping("/saveOrUpdate")
    public String saveOrUpdate(Appointment appointment) {
        if (appointment.getId() == null) {
            String yyyyMMddHHmmss = DateUtil.format(new Date(), "yyyyMMddHHmmss");
            String s = RandomUtil.randomNumbers(5);
            appointment.setAno(yyyyMMddHHmmss+s);
            System.out.println(yyyyMMddHHmmss+s);
            appointment.setStatus(1);
            appointmentService.save(appointment);
        } else {
            appointmentService.update(appointment);
        }
        return "redirect:/appointment/list";
    }

    @RequestMapping("/saveStatus")
    public String saveAno(Integer status,Long id){


        appointmentService.saveStatus(status,id);
        return "redirect:/appointment/list";
    }


/*    @Request(name="业务预约大类新增/编辑",expression="appointment:saveOrUpdate")
    @RequestMapping("/input")
    public String input(Long id, Model model) {
        if (id != null) {
            model.addAttribute("appointment",appointmentService.get(id));
        }
        return "appointment/input";
    }*/
}
